import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ArticleEntity } from '../entitys/article.entity';

@Injectable()
export class ArticleService {
  constructor(
    @InjectRepository(ArticleEntity)
    private readonly articleRepository: Repository<ArticleEntity>,
  ) {}

  async getArticleListServe() {
    const sql = "SELECT * FROM t_article WHERE f_is_del ='0'";
    return await this.articleRepository.query(sql);
  }

  async getArticleTypeServe() {
    const sql =
      'SELECT f_id as fId,f_type_name as fTypeName FROM t_article_type';
    return await this.articleRepository.query(sql);
  }

  async addArticleServe(articleContent) {
    const { fTitle, fAuthor, fContent, fTypeId } = articleContent;
    const sql =
      'INSERT INTO t_article(f_title,f_author,f_content,f_type_id) VALUE(?,?,?,?)';
    return await this.articleRepository.query(sql, [
      fTitle,
      fAuthor,
      fContent,
      fTypeId,
    ]);
  }

  async getArticleConByIdServe(articleId) {
    const sql = 'SELECT * FROM t_article WHERE f_id = ?';
    return await this.articleRepository.query(sql, articleId.fId);
  }
}
